package com.sd.coupon.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.sd.coupon.entity.CategoryGoods;
import com.sd.coupon.mapper.CategoryGoodsMapper;
import com.sd.coupon.model.res.CategoryRes;
import com.sd.coupon.service.ICategoryGoodsService;
//import com.sd.coupon.service.IPictureService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lws
 * @since 2018-12-04
 */
@Service
public class CategoryGoodsServiceImpl extends ServiceImpl<CategoryGoodsMapper, CategoryGoods> implements ICategoryGoodsService {

//	@Resource
//	IPictureService pictureService;
	
	@Override
	public List<CategoryRes> getCategoryTree(){


		try {//查询所有菜单
			List<CategoryRes> all = this.getAllDto();
			//根节点
			List<CategoryRes> rootCategoryDto = new ArrayList<CategoryRes>();
			for (CategoryRes nav : all) {
				if(nav.getPid()==0){//父节点是0的，为根节点。
					rootCategoryDto.add(nav);
				}
			}
			/* 根据CategoryDto类的order排序 */
			Collections.sort(rootCategoryDto, order());
			//为根菜单设置子菜单，getClild是递归调用的
			for (CategoryRes nav : rootCategoryDto) {
				/* 获取根节点下的所有子节点 使用getChild方法*/
				List<CategoryRes> childList = getChild(nav.getId(), all);
				nav.setChildren(childList);//给根节点设置子节点
			}
			/**
			 * 输出构建好的菜单数据。
			 *
			 */
			return rootCategoryDto;
		} catch (Exception e) {
			return null;
		}
//		return null;
	}

	public Comparator<CategoryRes> order(){
		Comparator<CategoryRes> comparator = new Comparator<CategoryRes>() {
			@Override
			public int compare(CategoryRes o1, CategoryRes o2) {
				if(o1.getSort() != o2.getSort()){
					return o1.getSort() - o2.getSort();
				}
				return 0;
			}
		};
		return comparator;
	}

//	@Cacheable()
	@Override
	public List<CategoryGoods> getAll(){
		QueryWrapper<CategoryGoods> queryWrapper = new QueryWrapper<CategoryGoods>();
		List<CategoryGoods> list = this.list(queryWrapper);
		return list;
	}
	
	private List<CategoryRes> getAllDto(){
		List<CategoryGoods> list = this.getAll();

		List<CategoryRes> extendsList = new ArrayList<CategoryRes>();
		for (CategoryGoods categoryGoods : list) {
			CategoryRes cate = new CategoryRes(categoryGoods);
			cate.setPic("https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/tam-ogel/f2e1aff288b72ad1a2b4ebec3c44f551_121_121.jpg");
			extendsList.add(cate);
		}
		return extendsList;
	}

	
	public List<CategoryRes> getChild(Integer id, List<CategoryRes> allCategoryDto){
	    //子菜单
	    List<CategoryRes> childList = new ArrayList<CategoryRes>();
	    for (CategoryRes nav : allCategoryDto) {
	      // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
	      //相等说明：为该根节点的子节点。
	      if(nav.getPid()==id){
	        childList.add(nav);
	      }
	    }
	    //递归
	    for (CategoryRes nav : childList) {
	      nav.setChildren(getChild(nav.getId(), allCategoryDto));
	    }
	    Collections.sort(childList,order());//排序
	    //如果节点下没有子节点，返回一个空List（递归退出）
	    if(childList.size() == 0){
	      return new ArrayList<CategoryRes>();
	    }
	    return childList;
	  }
}
